package com.copa.gamestore.module.gameNews.utils;

import com.copa.gamestore.common.utils.JsoupUtils;
import com.copa.gamestore.module.gameNews.domain.GameNews;
import com.copa.gamestore.module.gameNews.domain.GameNewsDetail;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

/**
 * @author copa
 * @createDate 2022-09-25 21:12
 * @function
 */
@Component
public class GameTimeUtils {

    /**
     * 游戏介绍
     * @param year
     * @param month
     * @return
     */
    public List<GameNews> getGamesInfo(String year, String month) {
        String url = "https://www.vgtime.com/game/release.jhtml?&platform=0&gene_list=" +
                "&pubtime=" + year + "-" + month + "&version=0&has_chinese_simple=0&has_chinese_dubbing=0";
        List<GameNews> result = new ArrayList<>();
        try {
            Document document = JsoupUtils.getBody(url);
            if (Objects.isNull(document)) {
                return null;
            }
            Elements oneGameTimesEle = document.getElementsByClass("vg_ss_box");
            for (Element oneDay : oneGameTimesEle) {
                String time = oneDay.getElementsByClass("ss_tit").text();
                if (time.length() < 8) {
                    continue;
                }
                Elements gameInfo = oneDay.getElementsByTag("dd");
                for (Element oneGame : gameInfo) {
                    String imgUrl = oneGame.getElementsByClass("img_box").get(0).getElementsByTag("img").attr("src")
                            .substring(0, oneGame.getElementsByClass("img_box").get(0).getElementsByTag("img").attr("src").lastIndexOf("?x-oss"));
                    String detail = oneGame.getElementsByClass("img_box").get(0).getElementsByTag("a").attr("href");
                    Element nameEle = oneGame.getElementsByClass("info_box").get(0);
                    String chiName = nameEle.getElementsByTag("h2").text();
                    String otherName = nameEle.getElementsByTag("span").text();
                    Elements descEle = oneGame.getElementsByClass("game_descri");
                    List<String> platformList = new ArrayList<>();
                    List<String> gameGeneList = new ArrayList<>();
                    String platformStr = "";
                    String gameGeneStr = "";
                    for (Element desc : descEle) {
                        Elements platformEle = desc.getElementsByClass("platform_detail_box").get(0).getElementsByTag("span");
                        for (Element platform : platformEle) {
                            platformList.add(
                                    "true".equals(platform.attr("data-cn"))
                                            ? platform.text() + "(中)"
                                            : platform.text());
                        }
                        Elements gameGeneEle = desc.getElementsByClass("game_gene").get(0).getElementsByTag("span");
                        for (Element gameGene : gameGeneEle) {
                            gameGeneList.add(gameGene.text());
                        }
                        platformStr = String.join(",", platformList);
                        gameGeneStr = String.join(",", gameGeneList);
                    }
                    result.add(new GameNews().setImgUrl(imgUrl)
                            .setGameGene(gameGeneStr)
                            .setPlatform(platformStr)
                            .setSaleTime(LocalDate.parse(time))
                            .setChiName(chiName)
                            .setOtherName(otherName)
                            .setDetailUrl("https://www.vgtime.com" + detail));
//                    result.add(GameNews.builder()
//                            .imgUrl(imgUrl)
//                            .gameGene(gameGeneStr)
//                            .platform(platformStr)
//                            .saleTime(LocalDate.parse(time))
//                            .chiName(chiName)
//                            .otherName(otherName)
//                            .detailUrl("https://www.vgtime.com" + detail)
//                            .build());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 游戏详情
     */
    public GameNewsDetail getGamesInfoDetail(String url) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            Document document = JsoupUtils.getBody(url);
            if (Objects.isNull(document)) {
                return null;
            }
            Elements gameMain = document.getElementsByClass("game_main");
            for (Element info : gameMain) {
                // 开发商、发行商
                Element gameInfoBox = info.getElementsByClass("game_info_box").get(0);
                Element developCompanyEle = gameInfoBox.getElementsByClass("descri_box").get(gameInfoBox.getElementsByClass("descri_box").size() - 2);
                String developCompany = "";
                if ("开发商".equals(developCompanyEle.getElementsByTag("p").text())) {
                    developCompany = developCompanyEle.getElementsByTag("span").text();
                    if (developCompany.isEmpty()) {
                        developCompany = developCompanyEle.getElementsByTag("a").text();
                    }
                }
                Element publishCompanyEle = gameInfoBox.getElementsByClass("descri_box").get(gameInfoBox.getElementsByClass("descri_box").size() - 1);
                String publishCompany = "";
                if ("发行商".equals(publishCompanyEle.getElementsByTag("p").text())) {
                    publishCompany = publishCompanyEle.getElementsByTag("a").text();
                    if (publishCompany.isEmpty()) {
                        publishCompany = publishCompanyEle.getElementsByTag("span").text();
                    }
                }
                // 多图片
                List<String> picImgList = new ArrayList<>();
                if (info.getElementsByClass("game_focus_list").size() > 0) {
                    Element gameFocusList = info.getElementsByClass("game_focus_list").get(0);
                    gameFocusList.getElementsByTag("li").forEach(li ->
                            picImgList.add(li.getElementsByTag("img").attr("data-source"))
                    );
                }
                // 游戏内容
                Map<String, Object> gameContent = new LinkedHashMap<>();
                Elements gameBoxList = info.getElementsByClass("game_box_list");
                for (Element gameBox : gameBoxList) {
                    String title = gameBox.getElementsByTag("h2").text();
                    switch (title) {
                        case "关于游戏" :
                        case "编辑的话" :
                            gameContent.put(title, gameBox.getElementsByTag("p").text());
                            break;
                    }
                }
                return new GameNewsDetail()
                        .setDevelopCompany(developCompany)
                        .setPublishCompany(publishCompany)
                        //.picUrl(mapper.writeValueAsString(picImgList))
                        //.gameContent(mapper.writeValueAsString(gameContent))
                        .setPicUrl(picImgList)
                        .setGameContent(gameContent);

//                return GameNewsDetail.builder()
//                        .developCompany(developCompany)
//                        .publishCompany(publishCompany)
//                        //.picUrl(mapper.writeValueAsString(picImgList))
//                        //.gameContent(mapper.writeValueAsString(gameContent))
//                        .picUrl(picImgList)
//                        .gameContent(gameContent)
//                        .build();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
